using System;
using System.Collections.Generic;
using System.Text;
using NUnit.Framework;
using TelnetExpect;
using System.IO;

namespace TelnetTests
{
    public class TelnetXStream_Write_Tests_OneByteAtATime : TelnetXStream_Write_Tests
    {
        protected override void Modifier_Write(TelnetStream telnet, byte[] data, int offset, int count)
        {
            byte[] tmp = new byte[1];
            for (int i = 0; i < data.Length; ++i) {
                tmp[0] = data[i];
                base.Modifier_Write(telnet, tmp, 0, 1);
            }
        }
    }


    public class TelnetXStream_Write_Tests_Offset100 : TelnetXStream_Write_Tests
    {
        protected override void Modifier_Write(TelnetStream telnet, byte[] data, int offset, int count)
        {
            const int ExtraOffset = 100;
            byte[] tmp = new byte[ExtraOffset + count];
            Array.Copy(data, offset, tmp, ExtraOffset, count);
            base.Modifier_Write(telnet, tmp, ExtraOffset, count);
        }
    }


    [TestFixture]
    public class TelnetXStream_Write_Tests
    {
        protected virtual void Modifier_Write(TelnetStream telnet, byte[] data, int offset, int count)
        {
            telnet.Write(data, offset, count);
        }

        void DoTest(byte[] writeData, byte[] expected)
        {
            MemoryStream dst = new MemoryStream();
            MemoryStream src = new MemoryStream();
            PairStream strm = new PairStream(src, dst);
            TelnetStream telnet = new TelnetStream(strm);
            Modifier_Write(telnet, writeData, 0, writeData.Length);
            TestsBase.Assert_Buffers_AreEqual(expected, dst, "Written");
        }

        //--------
        [Test]
        public void Aaaa()
        {
            byte[] data = { 1, 2, 3, 4, 5, 6, 7, };
            DoTest(data, data);
        }

        [Test]
        public void With255()
        {
            byte[] data = { 1, 2, 3, 255, 4, 5, 6, 7, };
            byte[] expected = { 1, 2, 3, 255, 255, 4, 5, 6, 7, };
            DoTest(data, expected);
        }

        [Test]
        public void All255()
        {
            byte[] data = { 255, 255, 255, 255, 255, };
            byte[] expected = { 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, };
            DoTest(data, expected);
        }

        [Test]
        public void LookLikeOptions()
        {
            byte[] data = { TestsBase.IAC, TestsBase.WILL, 99, TestsBase.IAC, TestsBase.SB, 18, 0, TestsBase.IAC, TestsBase.SE, };
            byte[] expected = { TestsBase.IAC, TestsBase.IAC, TestsBase.WILL, 99, 
                TestsBase.IAC, TestsBase.IAC, TestsBase.SB, 18, 0, TestsBase.IAC, TestsBase.IAC, TestsBase.SE, };
            DoTest(data, expected);
        }

    }
}
